package com.littlepay.kr4.paulin.dfeba77.utils;

/**
 * Created by john on 2017/5/15.
 */

import android.content.Context;
import android.content.SharedPreferences;

import com.littlepay.kr4.paulin.dfeba77.application.MyApplication;


/**
 * SharedPreferences工具类
 * Created by john on 2017/5/15.
 */
public class PrefUtils {

    //日间或者夜间模式
    //private static final String PRE_THEME_MODE = "dark_mode";
    //省流量模式 这儿和R.string.save_net_mode相同
    private static final String IS_CONTINUE = "is_continue";
    private static final String PRE_SAVE_REMIND_MODE = "save_remind_mode";
    private static final String PRE_SAVE_SYNC_MODE = "save_sync_mode";
    private static final String PRE_NAME = "com.littlepay.kr4.paulin.dfeba77_preferences";
    private static final String Name = "thenameis";
    private static final String PASSWORD = "password";
    private static final String IS_LOCKED = "islocked";

    private static SharedPreferences getSharedPreferences() {
        return MyApplication.getInstance().getContext()
                .getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
    }

//    public static boolean isDarkMode() {
//        return getSharedPreferences().getBoolean(PRE_THEME_MODE, false);
//    }

    /**
     * 隐私模式是否开启
     *
     * @param isLocked true为开启了验证密码
     */
    public static void setLocked(boolean isLocked) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(IS_LOCKED, isLocked);
        editor.apply();
    }

    public static boolean getLocked() {
        return getSharedPreferences().getBoolean(IS_LOCKED, false);
    }

    /**
     * 隐私模式密码
     *
     * @param password 传入设置的密码
     */
    public static void setPassword(String password) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(PASSWORD, password);
        editor.apply();
    }

    public static String getPassword() {
        return getSharedPreferences().getString(PASSWORD, "000000");
    }
    /**
     * 夜间模式
     *
     * @param isDarkMode true为夜间模式
     */
//    public static void setDarkMode(boolean isDarkMode) {
//        SharedPreferences.Editor editor = getSharedPreferences().edit();
//        editor.putBoolean(PRE_THEME_MODE, isDarkMode);
//        editor.apply();
//    }

    /**
     * 登录判断
     *
     * @param setlogin true为已经登录
     */
    public static void setLogin(boolean setlogin) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean("getLogin", setlogin);
        editor.apply();

    }

    /*
     *获取是否登录
     * return boolean
     */
    public static boolean getLogin() {
        return getSharedPreferences().getBoolean("getLogin", false);
    }

    /**
     * 是否开启连续记账模式
     *
     * @param isset true开启连续记账模式
     */
    public static void setContinuousSticker(boolean isset) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(IS_CONTINUE, isset);
        editor.apply();
    }

    public static boolean getContinuousSticker() {
        return getSharedPreferences().getBoolean(IS_CONTINUE, false);
    }

    /*
     * 是否开启定时提醒功能
     * @param isset true为开启提醒功能
     * */
    public static void remindMode(boolean isset) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(PRE_SAVE_REMIND_MODE, isset);
        editor.apply();
    }

    /**
     * 获得是否开启了定时提醒功能
     *
     * @return
     */
    public static boolean isRemindMode() {
        return getSharedPreferences().getBoolean(PRE_SAVE_REMIND_MODE, false);
    }

    /**
     * 是否开启仅在WiFi下同步功能
     *
     * @param isset true为开启wifi下同步功能
     */
    public static void wifi_sync(boolean isset) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(PRE_SAVE_SYNC_MODE, isset);
        editor.apply();
    }

    /**
     * 获得是否开启了仅在WIFI环境下同步的功能
     *
     * @return
     */
    public static boolean iswifi_sync() {
        return getSharedPreferences().getBoolean(PRE_SAVE_SYNC_MODE, false);
    }

    /**
     * 设置wifi同步功能
     *
     * @param isset true 开启仅在WIFI环境下同步的功能
     */
    public static void setwifi_sync(boolean isset) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(PRE_SAVE_SYNC_MODE, isset);
        editor.apply();
    }

    /*
     * 获取用户的昵称
     * @return name
     * */
    public static String getname() {
        SharedPreferences sp = MyApplication.getInstance().getContext().getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
        return sp.getString("rename", null);
    }

    /**
     * 获得预算金额
     *
     * @return
     */
    public static String getBudget() {
        SharedPreferences sp = MyApplication.getInstance().getContext().getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
        return sp.getString("budget", null);
    }

    /**
     * 设置预算
     *
     * @param budget
     */
    public static void setBudget(String budget) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString("budget", budget);
        editor.apply();
    }

    /**
     * 设置昵称
     *
     * @param name
     */
    public static void setRename(String name) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString("rename", name);
        editor.apply();

    }

    /**
     * 获得用户昵称
     *
     * @return
     */
    public static String getName() {
        return getSharedPreferences().getString("rename", null);

    }

    /**
     * 用于备份时间
     */
    public String getBackUpTime() {
        SharedPreferences preference = getSharedPreferences();
        String backuptime = preference.getString("back_up_time", "");
        return backuptime;
    }

    /**
     * 设置备份时间
     *
     * @param backuptime
     */
    public void setBackUpTime(String backuptime) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString("back_up_time", backuptime);
        editor.apply();
    }

    /**
     * use for uid
     */
    public static void setUid(String uid) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString("uid", uid);
        editor.apply();
    }

    /**
     * 获得当前的用户ID
     *
     * @return
     */
    public static String getUid() {
        SharedPreferences preference = getSharedPreferences();
        String uid = preference.getString("uid", "");
        return uid;
    }
    /*******************************************************/


    /******** set current id for download from the cloud***/
    /**
     * 设置当前数据库同步的Id号
     *
     * @param CurrentId
     */
    public static void setCurrentId(long CurrentId) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putLong("CurrentId", CurrentId);
        editor.apply();
    }

    public static long getCurrentId() {
        SharedPreferences preferences = getSharedPreferences();
        long CurrentId = preferences.getLong("CurrentId", 0);
        return CurrentId;
    }
    /*************************************************/

    /**use for cusror position
     ***/
//    out of some reasons,we don't need to use it,but save it in case we need it in the future
//    public void setPosition(int position){
//        SharedPreferences.Editor editor = getSharedPreferences().edit();
//        editor.putInt("position",position);
//        editor.apply();
//    }
//
//    public  int getPosition(){
//        SharedPreferences preference = getSharedPreferences();
//        int position = preference.getInt("position",0);
//        return position;
//    }
/**************************************************************************/

    /**
     * 删除 SharedPreferences 的某个 key
     *
     * @param key
     */
    public static void removeFromPrefs(String key) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.remove(key);

        editor.remove(key);
        editor.apply();
        editor.commit();

    }

    /**
     * 初始化数据库同步的ID号
     */
    public static void initcurrentID() {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString("currentID", "000000");
        editor.apply();
    }

    /**
     * 设置数据库同步当前ID
     *
     * @param currentID
     */
    public static void setcurrentID(String currentID) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString("currentID", currentID);
        editor.apply();
    }

    /**
     * 获得当前数据库同步的ID号
     *
     * @return
     */
    public static String getcurrentID() {
        return getSharedPreferences().getString("currentID", null);
    }

    /**
     * 设置剩余的金额
     *
     * @param remainmoney
     */
    public static void setremainmoney(int remainmoney) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString("remainmoney", String.valueOf(remainmoney));
        editor.apply();
    }

    /**
     * 获得剩余的金额
     *
     * @return
     */
    public static int getremainmoney() {
        return Integer.parseInt(getSharedPreferences().getString("remainmoney", null));
    }

    /**
     * 获得是否第一次打开软件
     *
     * @return
     */
    public static boolean getwelcome() {
        return getSharedPreferences().getBoolean("welcome", true);
    }

    /**
     * 设置是否第一次登录软件
     *
     * @param set
     */
    public static void setwelcome(boolean set) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean("welcome", set);
        editor.apply();
    }
}
